package fr.upmc.tps.mdoc.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.upmc.tps.mdoc.domain.DAOContact;
import fr.upmc.tps.mdoc.domain.IDAOContact;
import fr.upmc.tps.mdoc.models.IContact;

/**
 * Servlet implementation class DeleteContact
 */
public class DeleteContact extends HttpServlet {

	private static final long serialVersionUID = -634584410467182156L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeleteContact() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		long id = Long.parseLong(request.getParameter("id"));

		request.getRequestDispatcher("pages/deleteContact.jsp?id=" + id)
				.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		IDAOContact dao = new DAOContact();

		long contactId = Long.parseLong(request.getParameter("id"));

		IContact contact;
		if (dao.getEntreprise(contactId) != null) {
			
			contact = dao.getContact(contactId);
			//((Entreprise)contact).setNumSiret(0);
			
		} else {

			contact = dao.getContact(contactId);
		}

		if (dao.deleteContact(contact)) {

			RequestDispatcher rd = request
					.getRequestDispatcher("pages/accueil.jsp");

			rd.forward(request, response);
		}

	}

}
